<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/11/6
 * Time: 11:01
 */
namespace app\shop\controller;
use think\Db;
use think\Request;
class Account extends Base{

    public function rolePage(){
        $data = Db::name('role_name')->alias('a')
            ->join('up_role ur','ur.role_id = a.f_id','left')
            ->where('r_s_id',session('shop_id'))
            ->order('role_name_id desc')
            ->field('a.*,ur.role_name as role_name1')
            ->select();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function add(){
       $data =  Db::table('up_role')
           ->where('role_s_id',session('shop_id'))
           ->select();
        $this->assign('data',$data);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 添加账号密码
     */
    public function releaseTask(){
        if(request ()->isPost()){
            $account = input('param.account');
            if(!preg_match("/^1[3456789]\d{9}$/", $account) && strlen($account) < 6) {
                return json(['status' => 0, 'msg' => '请输入正确的手机号']);
            }
            if(!preg_match("/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,20}$/", input('param.password'))) {
                return json(['status' => 0, 'msg' => '请输入正确的密码']);
            }
            $info = Db::table('up_role_name')->where('role_user',$account)->find();
            if($info['role_name_id']!=''){
                return json(['status' => 100, 'msg' => '手机号码已经存在,请不要重复添加']);
            }
            $info1 = Db::table('up_shops')->where('s_phone',$account)->find();
            if($info1['s_id']!=''){
                return json(['status' => 100, 'msg' => '手机号码已经存在,请不要重复添加']);
            }
            $data['role_name'] = input('param.username');
            $data['role_user'] = input('param.account');
            $data['role_into'] = input('param.intro');
            $data['role_time'] = date('Y-m-d H:i:s');
            $data['f_id'] = input('param.role');
            $data['role_password'] = md5(input('param.password'));
            $data['password'] = input('param.password');
            $data['r_s_id'] = session('shop_id');
            Db::table('up_role_name')->insert($data);
            return json(['status' => 200, 'msg' => '成功']);
        }
    }

    public function update(){
        $id = input('param.id');
        $data = Db::table('up_role_name')->where('role_name_id',$id)->find();
        $data1 =  Db::table('up_role')->where('role_s_id',session('shop_id'))->select();
        $this->assign('data1',$data1);
        $this->assign('data',$data);
        $this->assign('id',$id);
        return $this->fetch();
    }
    public function updateTask(){
        if(request ()->isPost()){
            $id = input('param.id');
            $account = input('param.account');
            if(!preg_match("/^1[3456789]\d{9}$/", $account) && strlen($account) < 6) {
                return json(['status' => 0, 'msg' => '请输入正确的手机号']);
            }
            if(!preg_match("/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,20}$/", input('param.password'))) {
                return json(['status' => 0, 'msg' => '请输入正确的密码']);
            }
            $where['role_user'] = $account;
            $where['role_name_id'] = array('neq',$id);
            $info = Db::table('up_role_name')->where($where)->find();
            if($info['role_name_id']!=''){
                return json(['status' => 100, 'msg' => '手机号码已经存在,请不要重复添加']);
            }
            $info1 = Db::table('up_shops')->where('s_phone',$account)->find();
            if($info1['s_id']!=''){
                return json(['status' => 100, 'msg' => '手机号码已经存在,请不要重复添加']);
            }
            $data['role_name'] = input('param.username');
            $data['role_user'] = input('param.account');
            $data['role_into'] = input('param.intro');
            $data['role_time'] = date('Y-m-d H:i:s');
            $data['f_id'] = input('param.role');
            $data['role_password'] = md5(input('param.password'));
            $data['password'] = input('param.password');
            Db::table('up_role_name')->where('role_name_id',$id)->update($data);

            return json(['status' => 200, 'msg' => '成功']);
        }
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $id = input('param.id');
        $res = Db::table('up_role_name')->where('role_name_id',$id)->delete();
        if($res){
            return json(['status'=>200,'msg'=>'删除成功']);
        }else{
            return json(['status'=>100,'msg'=>'删除失败']);
        }
    }
    public function status(){
        $type= input('param.type');
        $id = input('param.id');
        if($type==1){
            $res = Db::table('up_role_name')->where('role_name_id',$id)->setField('role_status',1);
        }else{
            $res = Db::table('up_role_name')->where('role_name_id',$id)->setField('role_status',2);
        }
        if($res){
            return json(['status'=>200,'msg'=>'成功']);
        }else{
            return json(['status'=>100,'msg'=>'失败']);
        }
    }
}